package com.one.consumer.user.ui;

import android.content.Intent;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.model.extra.BaseExtra;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.view.base.IListView;
import com.one.common.view.widget.MyTitleBar;
import com.one.consumer.BaseListActivityC;
import com.one.consumer.R;
import com.one.consumer.reserve.ui.activity.AddServiceActivity;
import com.one.consumer.user.model.extra.AddressExtra;
import com.one.consumer.user.model.item.AddressItem;
import com.one.consumer.user.presenter.AddressListPresenter;
import com.one.consumer.user.ui.binder.AddressBinder;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/28 3:33 PM
 * description ：选择地址
 **/
@Route(path = RouterPath.SELECT_ADDRESS)
public class SelectAddressActivity extends BaseListActivityC<AddressListPresenter> implements IListView, AddressBinder.AddressClickListener {
    private DefaultExtra extra;

    @Override
    protected void initPresenter() {
        mPresenter = new AddressListPresenter(this, this);
    }

    @Override
    public void registerDate() {
        register(AddressItem.class, new AddressBinder(this));
    }

    @Override
    public void loadData() {
        mPresenter.getAddressList();
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        extra = (DefaultExtra) getIntent().getSerializableExtra(DefaultExtra.getExtraName());
        String title = "地址管理";
        if (extra != null) {
            title = "选择地址";
        }
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE_RIMG)
                .setTitleText(title)
                .setRightImg(R.mipmap.ic_add_black);
    }

    @Override
    protected void onClickRightImg() {
        super.onClickRightImg();
        RouterManager.getInstance().go(RouterPath.ADD_ADDRESS, new AddressExtra(AddressExtra.ADD));
    }

    @Override
    public boolean onEmpty() {
        return true;
    }

    @Override
    public void onAddressClick(AddressItem item) {
        if (extra != null) {
            Intent intent = new Intent();
            intent.putExtra(BaseExtra.getExtraName(), item);
            setResult(AddServiceActivity.RESULT_ADDRESS, intent);
            finishPage();
        } else {
            RouterManager.getInstance().go(RouterPath.ADD_ADDRESS,
                    new AddressExtra(AddressExtra.UPDATE, item));
        }
    }

    @Override
    public void onDelete(AddressItem item) {
        doRefresh();
    }

    @Override
    protected void onResume() {
        super.onResume();
        doRefresh();
    }
}
